package com.zby.demo.controller;

import com.zby.demo.dao.StudentDao;
import com.zby.demo.dao.TeacherDao;
import com.zby.demo.entity.Student;
import com.zby.demo.entity.Teacher;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @Author ChrisPhantom
 * @CreateDate 2020/12/13 18:15
 */

@Controller
public class LoginController {
    @Autowired
    StudentDao studentDao;
    @Autowired
    TeacherDao teacherDao;

    /*
     * @Param: []
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/18 18:21
     * @Description: 请求转发到登录页面
     */
    @RequestMapping("/index.html")
    public String toLogin(HttpSession session) {
        if (session.getAttribute("LoginUser") != null) {
            return "redirect:/back";
        }
        return "login";
    }

    @Value("${administrator.username}")
    String admin_useranme;
    @Value("${administrator.password}")
    String admin_password;

    /*
     * @Param: [username, password, model, session]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/18 18:22
     * @Description: 判断用户是否登录成功（管理员账号密码存储在yml配置文件里），若登录成功则重定向到对应的首页；否则请求转发回登录页面，将错误信息传回页面显示（并将账号密码回传用于回填）
     */
    @PostMapping("/login")
    public String checkLogin(@Param("username") String username, @Param("password") String password, Model model, HttpSession session) {
        if (username.equals(admin_useranme) && password.equals(admin_password)) {
            session.setAttribute("LoginUser", username);
            session.setAttribute("classify", "administrator");
            return "redirect:/back/index.html";
        } else {
            Student student = studentDao.queryStudentByUsername(username);
            Teacher teacher = teacherDao.quertyTeacherByID(username);
            if (student == null && teacher == null) {
                model.addAttribute("msg", "账号不存在，如有问题请联系管理员");
                model.addAttribute("username", username);
                model.addAttribute("password", password);
                return "login";
            }
            String sp = "";
            String tp = "";
            if (student != null) {
                sp = student.getPassword();
            }
            if (teacher != null) {
                tp = teacher.getPassword();
            }
            if (password.equals(sp)) {
                session.setAttribute("LoginUser", username);
                session.setAttribute("classify", "student");
                session.setAttribute("name", student.getStudentName());
                return "redirect:/front/student";
            } else if (password.equals(tp)) {
                session.setAttribute("LoginUser", username);
                session.setAttribute("classify", "teacher");
                session.setAttribute("name", teacher.getTeacherName());
                return "redirect:/front/teacher";
            } else {
                model.addAttribute("msg", "账号或密码错误");
                model.addAttribute("username", username);
                model.addAttribute("password", password);
                return "login";
            }
        }
    }

    /*
     * @Param: [session]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/18 18:22
     * @Description: 登出功能，清除session内容吗，然后重定向到登录处理
     */
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("LoginUser");
        session.removeAttribute("classify");
        session.removeAttribute("name");
        return "redirect:/index.html";
    }
}
